package vn.edu.ptithcm.pim.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import vn.edu.ptithcm.pim.dom.Customer;
import vn.edu.ptithcm.pim.query.CustomerQuery;

@SuppressWarnings("rawtypes")
public class CustomerDao extends HibernateDaoSupport {

	public void save(Customer customer) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().save(customer);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void saveOrUpdate(Customer customer) {
		try {
			Transaction tx = getSession().beginTransaction();
			if (customer.getCustomerid() != null) {
				getHibernateTemplate().merge(customer);
			} else {
				getHibernateTemplate().saveOrUpdate(customer);
			}
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void lock(Customer customer) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().lock(customer, LockMode.NONE);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void delete(Customer customer) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().delete(customer);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public Customer merge(Customer customer) {
		try {
			return (Customer) getHibernateTemplate().merge(customer);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public Customer findById(java.lang.Long id) {
		try {
			return (Customer) getHibernateTemplate().get(Customer.class, id);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findByExample(Customer customer) {
		try {
			return getHibernateTemplate().findByExample("Customer", customer);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List getAll() {
		try {
			return getHibernateTemplate().loadAll(Customer.class);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void deleteById(java.lang.Long id) {
		try {
			Transaction tx = getSession().beginTransaction();
			StringBuilder sql = new StringBuilder();
			sql.append(
					"delete Customer as customer where customer.customerid ='")
					.append(id).append("'");
			getHibernateTemplate().bulkUpdate(sql.toString());
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Customer> findByQuery(CustomerQuery query, int max, int offset) {
		try {
			String searchKey = StringUtils.trimToEmpty(query.getSearchKey());
			if ("Keyword...".equals(searchKey)) {
				searchKey = "";
			}
			Criteria c = getSession().createCriteria(Customer.class);
			if (!searchKey.isEmpty()) {
				c.add(Restrictions.ilike("name", "%" + searchKey + "%"));
			}
			c.addOrder(Order.asc("name"));
			return c.setFirstResult(offset).setMaxResults(max).list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public int countByQuery(CustomerQuery query) {
		try {
			String searchKey = StringUtils.trimToEmpty(query.getSearchKey());
			if ("Keyword...".equals(searchKey)) {
				searchKey = "";
			}
			Criteria c = getSession().createCriteria(Customer.class);
			if (!searchKey.isEmpty()) {
				c.add(Restrictions.ilike("name", "%" + searchKey + "%"));
			}
			c.setProjection(Projections.rowCount());
			return Integer.parseInt(c.list().get(0).toString());
		} catch (RuntimeException re) {
			throw re;
		}
	}
}
